package com.ruicar.afs.cloud.afscase.autoaudit.controller;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruicar.afs.cloud.afscase.autoaudit.condition.LoanRiskTipsRuleCondition;
import com.ruicar.afs.cloud.afscase.autoaudit.entity.LoanRiskTipsRule;
import com.ruicar.afs.cloud.afscase.autoaudit.mapper.LoanRiskTipsRuleMapper;
import com.ruicar.afs.cloud.afscase.autoaudit.service.LoanRiskTipsRuleService;
import com.ruicar.afs.cloud.bizcommon.rules.service.AfsRuleInfoService;
import com.ruicar.afs.cloud.common.core.util.IResponse;
import com.ruicar.afs.cloud.image.entity.ComAttachmentManagement;
import com.ruicar.afs.cloud.image.enums.AttachmentClassEnum;
import com.ruicar.afs.cloud.image.enums.BusiNodeEnum;
import com.ruicar.afs.cloud.image.enums.RuleStateEnum;
import com.ruicar.afs.cloud.image.mapper.ComAttachmentManagementMapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author tiankai
 * @Describe 放款风险提示规则接口
 * Created on 2020-05-15
 */
@Slf4j
@RestController
@RequestMapping("/loanRisk")
@AllArgsConstructor
@Api(description = "放款风险提示规则接口")
public class LoanRiskTipsRuleController {
    private LoanRiskTipsRuleService service;
    private ComAttachmentManagementMapper mapper;
    private LoanRiskTipsRuleMapper ruleMapper;
    private AfsRuleInfoService afsRuleInfoService;

    @RequestMapping(value = "/saveOrUpdateLoanRisk", method = RequestMethod.POST)
    @ApiOperation(value = "更新放款风险提示信息")
    public IResponse<LoanRiskTipsRule> saveOrUpdateLoanRisk(@RequestBody LoanRiskTipsRule tipsRule) {
        LoanRiskTipsRule riskTipsRule = service.getById(tipsRule.getId());
        ComAttachmentManagement management = mapper.selectById(tipsRule.getAttachmentId());
        if (ObjectUtil.isNull(riskTipsRule)) {
            riskTipsRule = new LoanRiskTipsRule();
            riskTipsRule.setRuleState(RuleStateEnum.EFFECTIVE.getCode());
            riskTipsRule.setRuleName(tipsRule.getRuleName());
            riskTipsRule.setRemarks(tipsRule.getRemarks());
        }
        riskTipsRule.setAttachmentId(tipsRule.getAttachmentId());
        riskTipsRule.setAttachmentName(management.getAttachmentName());
        riskTipsRule.setRuleName(tipsRule.getRuleName());
        riskTipsRule.setRemarks(tipsRule.getRemarks());
        service.saveOrUpdate(riskTipsRule);
        return new IResponse().setData(riskTipsRule);
    }

    @GetMapping(value = "/queryAttachmentClassList")
    @ApiOperation(value = "查询附小类信息")
    public IResponse<List<ComAttachmentManagement>> queryAttachmentClass() {
        int attachmentClass = AttachmentClassEnum.SUBCLASS.getCode();
        List<ComAttachmentManagement> list = mapper.selectList(Wrappers.<ComAttachmentManagement>query().lambda()
                .eq(ComAttachmentManagement::getAttachmentClass, attachmentClass)
                .eq(ComAttachmentManagement::getBusiNode, BusiNodeEnum.LOAN_APPLY.getCode()).orderByAsc(ComAttachmentManagement::getSortOrder));
        return IResponse.success(list);
    }

    @GetMapping(value = "/queryAttachmentClass")
    @ApiOperation(value = "查询附小类信息")
    public IResponse<List<ComAttachmentManagement>> queryAttachmentClass(@RequestParam(name = "pageNumber") int pageNumber, @RequestParam(name = "pageSize") int pageSize) {
        int attachmentClass = AttachmentClassEnum.SUBCLASS.getCode();
        IPage<List<ComAttachmentManagement>> list = mapper.selectPage(new Page(pageNumber, pageSize), Wrappers.<ComAttachmentManagement>query().lambda()
                .eq(ComAttachmentManagement::getAttachmentClass, attachmentClass)
                .eq(ComAttachmentManagement::getBusiNode, BusiNodeEnum.LOAN_APPLY.getCode()).orderByAsc(ComAttachmentManagement::getSortOrder));
        return IResponse.success(list);
    }

    @GetMapping(value = "/getLoanRuleInfo")
    @ApiOperation(value = "查询风险提示规则信息")
    public IResponse<IPage<LoanRiskTipsRuleCondition>> getLoanRuleInfo(@RequestParam(name = "pageNumber") int pageNumber, @RequestParam(name = "pageSize") int pageSize,
                                                                       @RequestParam(required = false) String attachmentName,String ruleState) {
        LoanRiskTipsRuleCondition ruleCondition = new LoanRiskTipsRuleCondition();
        ruleCondition.setAttachmentName(attachmentName);
        ruleCondition.setRuleState(ruleState);
        IPage<List<LoanRiskTipsRuleCondition>> list = ruleMapper.getLoanRuleInfo(new Page(pageNumber, pageSize), ruleCondition);
        return IResponse.success(list);
    }

    @RequestMapping(value = "/updateLoanRiskRule", method = RequestMethod.POST)
    @ApiOperation(value = "修改规则状态")
    public IResponse<LoanRiskTipsRule> updateLoanRiskRule(@RequestBody LoanRiskTipsRuleCondition condition) {
        LoanRiskTipsRule rule = service.getById(condition.getId());
        if (RuleStateEnum.EFFECTIVE.getCode().equals(condition.getRuleState())) {
            afsRuleInfoService.activeRule(condition.getRuleId());
            rule.setRuleState(RuleStateEnum.INVALID.getCode());
        } else if (RuleStateEnum.INVALID.getCode().equals(condition.getRuleState())) {
            afsRuleInfoService.deActiveRule(condition.getRuleId());
            afsRuleInfoService.expiredRule(condition.getRuleId());
            rule.setRuleState(RuleStateEnum.EFFECTIVE.getCode());
        } else {
            rule.setRuleState(RuleStateEnum.EFFECTIVE.getCode());
        }
        service.saveOrUpdate(rule);
        return new IResponse().setData(true);
    }
}
